layui.use(['table', 'form', 'func', 'HttpRequest', 'util'], function () {
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var func = layui.func;
    var HttpRequest = layui.HttpRequest;
    var util = layui.util;

    // 职位表管理
    var Training = {
        tableId: "trainingTable"
    };

    // 初始化表格的列
    Training.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'recordId', hide: true, title: '主键id'},
            {field: 'realName', sort: true, title: '姓名'},
            {field: 'account', sort: true, title: '账号'},
            {field: 'trainingTitle', sort: true, title: '培训标题'},
            {
                field: 'createTime', sort: true, title: '创建时间', templet: function (d) {
                    return util.toDateString(d.createTime);
                }
            },
            {
                field: 'updateTime', sort: true, title: '更新时间', templet: function (d) {
                    return d.updateTime == null ? '' : util.toDateString(d.updateTime);
                }
            },
            {field: 'statusFlag', sort: true, templet: '#statusTpl', title: '状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    // 点击查询按钮
    Training.search = function () {
        var queryData = {};
        queryData['account'] = $("#account").val();
        //queryData['positionCode'] = $("#positionCode").val();
        table.reload(Training.tableId, {
            where: queryData,
            page: {curr: 1}
        });
    };

    // 弹出添加对话框
    Training.openAddDlg = function () {
        func.open({
            title: '添加培训记录',
            content: Feng.ctxPath + '/view/training/addView',
            tableId: Training.tableId
        });
    };

    // 点击编辑
    Training.openEditDlg = function (data) {
        func.open({
            title: '修改培训记录',
            content: Feng.ctxPath + '/view/training/editView?recordId=' + data.recordId,
            tableId: Training.tableId
        });
    };

    // 点击查看
    Training.openDetailDlg = function (data) {
        func.open({
            title: '查看培训记录',
            content: Feng.ctxPath + '/view/training/detailView?recordId=' + data.recordId,
            tableId: Training.tableId
        });
    };

    // 审批
    Training.openAuditDlg = function (data) {
        func.open({
            title: '培训记录审批',
            content: Feng.ctxPath + '/view/training/auditView?recordId=' + data.recordId,
            tableId: Training.tableId
        });
    };

    // 导出excel按钮
    Training.exportExcel = function () {
        var checkRows = table.checkStatus(Training.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 点击删除
    Training.delete = function (data) {
        var operation = function () {
            var httpRequest = new HttpRequest(Feng.ctxPath + "/training/delete", 'post', function (data) {
                Feng.success("删除成功!");
                table.reload(Training.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            httpRequest.set(data);
            httpRequest.start(true);
        };
        Feng.confirm("是否删除?", operation);
    };

    // 点击删除
    Training.view = function (data) {
        var operation = function () {
            var httpRequest = new HttpRequest(Feng.ctxPath + "/training/delete", 'post', function (data) {
                Feng.success("删除成功!");
                table.reload(Training.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            httpRequest.set(data);
            httpRequest.start(true);
        };
        Feng.confirm("是否删除?", operation);
    };

    // 修改职位状态
    Training.updateStatus = function (positionId, checked) {
        var httpRequest = new HttpRequest(Feng.ctxPath + "/training/updateStatus", 'post', function (data) {
            table.reload(Training.tableId);
            Feng.success("修改成功!");
        }, function (data) {
            table.reload(Training.tableId);
            Feng.error("修改失败!" + data.message);
        });
        httpRequest.set({"positionId": positionId, "statusFlag": checked});
        httpRequest.start(true);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Training.tableId,
        url: Feng.ctxPath + '/training/page',
        page: true,
        request: {pageName: 'pageNo', limitName: 'pageSize'}, //自定义分页参数
        height: "full-158",
        cellMinWidth: 100,
        cols: Training.initColumn(),
        parseData: Feng.parseData
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Training.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Training.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Training.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Training.tableId + ')', function (obj) {
        var data = obj.data;
        var event = obj.event;
        if (event === 'edit') {
            Training.openEditDlg(data);
        } else if (event === 'delete') {
            Training.delete(data);
        } else if (event === 'view') {
            Training.openDetailDlg(data);
        } else if (event === 'audit') {
            Training.openAuditDlg(data);
        }
    });

    // 修改状态
    form.on('switch(status)', function (obj) {
        var positionId = obj.elem.value;
        var checked = obj.elem.checked ? 1 : 2;
        Training.updateStatus(positionId, checked);
    });
});
